package com.maxd.handler.pay;

import com.maxd.constants.PayScene;
import com.maxd.error.DescribeException;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.model.*;
import com.maxd.respository.selfGoods.GoodsJpaRepository;
import com.maxd.respository.selfGroup.SelfGroupJpaRepository;
import com.maxd.respository.selfGroupPink.SelfGroupPinkJpaRepository;
import com.maxd.respository.selfSeckill.SelfSeckillJpaRepository;
import com.maxd.service.selfOrder.OrdersService;
import com.maxd.service.selfWXPay.SelfWXService;
import com.maxd.utils.AssertUtil;
import com.maxd.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class OrderPayHandler implements IPayHandler {

    private static final Logger log = LoggerFactory.getLogger(OrderPayHandler.class);
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private GoodsJpaRepository goodsJpaRepository;
    @Autowired
    private SelfGroupJpaRepository selfGroupJpaRepository;
    @Autowired
    private SelfSeckillJpaRepository selfSeckillJpaRepository;
    @Autowired
    private SelfGroupPinkJpaRepository selfGroupPinkJpaRepository;

    @Autowired
    private SelfWXService selfWXService;

    @Override
    public String getScene() {
        return PayScene.ORDER_PAY.getCode();
    }

    @Override
    public void preHandler(PayRecord payRecord) {
        List<Orders> orders = ordersService.findByPayNum(payRecord.getPaySeq());
        log.info("OrderPayHandler preHandler, orderList = {}", JsonUtils.toJsonString(orders));
        for (Orders order : orders) {
            // 订单已经支付
            AssertUtil.assertTrue(order.getStatus() == 1, ExceptionEnum.ORDER_PAYED);
            // 这里也不校验了，直接用登录态的openId支付
            // AssertUtil.assertTrue(userId.equals(order.getUserId()), ExceptionEnum.DATA_CHANGED);
            if(order.getGroupId()!=null){
                SelfGroup selfGroup = selfGroupJpaRepository.findById(order.getGroupId()).orElse(null);
                AssertUtil.assertTrue(selfGroup != null && selfGroup.getStatus() == 1, ExceptionEnum.GOODS_NOT_AVAILABLE);
            }else if(order.getSecKillId()!=null){
                SelfSecKill selfSecKill = selfSeckillJpaRepository.findById(order.getSecKillId()).orElse(null);
                AssertUtil.assertTrue(selfSecKill != null && selfSecKill.getStatus() == 1, ExceptionEnum.GOODS_NOT_AVAILABLE);
            }else if(order.getGoodsId()!=null){
                Goods g = goodsJpaRepository.findById(order.getGoodsId()).orElse(null);
                AssertUtil.assertTrue(g != null && g.getStatus() == 1, ExceptionEnum.GOODS_NOT_AVAILABLE);
            }

            if (order.getGroupPinkId() != null) {
                SelfGroupPink selfGroupPink = selfGroupPinkJpaRepository.findById(order.getGroupPinkId()).orElse(null);
                AssertUtil.assertTrue(selfGroupPink.getStatus() != 2, ExceptionEnum.GROUP_FINISHED);
            }
        }
    }

    @Override
    public void postHandler(PayRecord payRecord) {

    }

    @Override
    public void callBackHandler(PayRecord payRecord) {
        selfWXService.wxPayBack(payRecord.getPaySeq(), 3);
    }

    @Override
    public void cancelHandler(PayRecord payRecord) {
        List<Orders> orders = ordersService.findByPayNum(payRecord.getPaySeq());
        for (Orders order : orders) {
            Result result = ordersService.orderCancel(order.getId());
            if(result.getStatus() != 0) {
                log.error("取消订单失败, orderId={}", order.getOrderNum());
            }
        }
    }
}
